var request = {};
var BasicAuthorizationCode = function(username, password){
    var safeStr = unescape(encodeURIComponent(username + ':' + password));
    var btoaCode = btoa(safeStr);
    return 'Basic ' + btoaCode;
};
(function (request, window) {

    request.settings = {};

    request.settings.fileParameterName = "file";

    request.post = function (url, obj, success, fail) {
        $.ajax(url, {
            type: 'POST',
            data: obj
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.authPost = function (url, obj, success, fail) {
        $.ajax({
            url:url,
            beforeSend: function (xhr) {
                xhr.setRequestHeader ('Authorization', BasicAuthorizationCode("tom", "123456"));
            },
            type: 'POST',
            data: obj
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.postObject = function (url, obj, success, fail) {
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
                xhrObj.setRequestHeader("Accept", "application/json; charset=UTF-8");
            },
            type: 'POST',
            data: JSON.stringify(obj)
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.postEntity = function (url, objectType, json, success, fail) {
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'POST',
            data: json
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.deleteEntity = function (url, objectType, id, success, fail) {
        url = url + '?' + $.param({id: id});
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'DELETE'
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.getEntity = function (url, objectType, id, success, fail) {
        url = url + '?' + $.param({id: id});
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'GET'
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.setEntity = function (url, objectType, id, json, success, fail) {
        url = url + '?' + $.param({id: id});
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'PUT',
            data: json
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.getAllEntities = function (url, objectType, page, size, success, fail) {
        url = url + '?' + $.param({page: page, size: size});
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'GET'
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.deleteAllEntities = function (url, objectType, xs, success, fail) {
        var s = xs.join(',');
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'DELETE',
            data: s
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.loadEntities = function (url, objectType, filepath, success, fail) {
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");
                xhrObj.setRequestHeader("Object-Type", objectType);
            },
            type: 'POST',
            data: filepath
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.serializeEntities = function (url, objectType, page, size, success, fail) {
        url = url + '?' + $.param({page: page, size: size});
        $.ajax(url, {
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Object-Type", objectType);
                xhrObj.setRequestHeader("Accept-Stream", true);
            },
            type: 'GET'
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.uploadFile = function (url, file, businessType, success, fail) {
        url = url + $.param({businessType: businessType});
        var formData = new FormData();
        formData.append(request.settings.fileParameterName, file);
        $.ajax(url, {
            type: 'POST',
            processData: false,
            contentType: false,
            data: formData
        }).done(function (data) {
            success(data);
        }).fail(function (jqXHR) {
            fail(jqXHR);
        });
    };

    request.downloadFile = function (url, filepath, filename) {
        url = url + '?' + $.param({filepath: filepath, filename: filename});
        window.location.href = url;
    };
})(request, this);